package com.gzyz.drug.controller;
import com.alibaba.fastjson.JSONObject;
import com.gzyz.drug.entity.*;
import com.gzyz.drug.service.ChinaTotalService;
import com.gzyz.drug.service.IndexService;
import com.gzyz.drug.service.NocvNewsService;
import com.gzyz.drug.service.TJService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class IndexController {
    @Autowired
    private IndexService indexService;
    @Autowired
    private ChinaTotalService chinaTotalService;
    @Autowired
    private NocvNewsService nocvNewsService;
    @Autowired
    private TJService tjService;

    /**
     * 跳转china地图页面
     *
     * @return
     */
    @RequestMapping("/toChina")
    public String toChina()  {
     return "/china";
    }



    /**
     * 查34个省份的数据
     * @return
     * @throws ParseException
     */
    @RequestMapping("/query")
    @ResponseBody
    public List<NocvData> queryData()  {
     List<NocvData> list = indexService.list();
     return list;

    }


    //    跳转pie页面
    @RequestMapping("/toPie")
    public String toPie(){
        return "/pie";
    }

    /**
     * 分组聚合
     *  SQL:select count(*) form goods group by type;
     */
//    @RequestMapping("/queryPie")
//    @ResponseBody
//    public List<NocvData> queryPieData(){
//        List<NocvData> list = indexService.listOrderByIsLimit34();
//        return list;
//    }
    @RequestMapping("/queryPie")
    @ResponseBody
    public List<TianJin> queryPieData(){
        List<TianJin> list = tjService.listOrderByIsLimit20();
        System.out.println(list);
        return list;
    }


    /**
     * 跳转bar页面（柱状图页面）
     */
    @RequestMapping("/toBar")
    public String toBar(){
        return "/bar";
    }

    @RequestMapping("/queryBar")
    @ResponseBody
    public Map<String,List<Object>> queryBarData(){
        //1.拿到所有城市数据：数值
        List<TianJin> list = tjService.listOrderByIsLimit();
        //2.所有城市数据
        List<String> cityList = new ArrayList<>();
        for (TianJin data: list){
            cityList.add(data.getName());
        }
        //3.所有疫情数值数据
        List<Integer> dataList = new ArrayList<>();
        for (TianJin data: list){
            dataList.add(data.getValue());
        }
        //4.创建Map
        Map map = new HashMap();
        map.put("areaList",cityList);
        map.put("dataList",dataList);

        return map;
    }


    /**
     * 跳转line页面（折线图页面）
     */
    @RequestMapping("/toLine")
    public String toLine(){
        return "/line";
    }

    /**
     * select * from 趋势数据表 order by create_time (asc) limit 7;
     */
    @RequestMapping("/queryLine")
    @ResponseBody
    public Map<String,List<Object>> queryLineData(){
        //1.查询近七天所有的数据
        List<LineTrend> list7 = indexService.findSevenData();
        //2.封装所有的确诊人数
        List<Integer> confirmList = new ArrayList<>();
        //3.封装所有的隔离人数
        List<Integer> isolationList = new ArrayList<>();
        //4.封装所有的治愈人数
        List<Integer> cureList = new ArrayList<>();
        //5.封装所有的死亡人数
        List<Integer> deadList = new ArrayList<>();
        //6.封装所有的疑似人数
        List<Integer> similarList = new ArrayList<>();
        for (LineTrend data : list7){
            confirmList.add(data.getConfirm());
            isolationList.add(data.getIsolation());
            cureList.add(data.getCure());
            deadList.add(data.getDead());
            similarList.add(data.getSimilar());
        }
        //7.返回数据的格式容器Map
        Map map = new HashMap<>();
        map.put("confirmList",confirmList);
        map.put("isolationList",isolationList);
        map.put("cureList",cureList);
        map.put("deadList",deadList);
        map.put("similarList",similarList);

        return map;
    }

}
